﻿@model TestUp.Models.AdminModels.ViewPendingTestsModel
@{
    ViewBag.Title = "ViewPendingTests";
    Layout = "~/Views/Admin/_AdminLayout.cshtml";
}

<script type="text/javascript">
    function destroy(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        var id = dataItem.GroupId;
        if (confirm("Sure you want to delete this pending test?")) {
            var form = document.createElement("form");
            form.setAttribute("method", "post");
            form.setAttribute("action", "DeletePendingTest");
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", "PendingTestId");
            hiddenField.setAttribute("value", id);
            form.appendChild(hiddenField);
            document.body.appendChild(form);
            form.submit();
        }
    }

    function edit(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        var id = dataItem.PendingTestId;

        var url = '@Url.Action("EditPendingTest", "Admin", new { id = "__id__" })';
        window.location.href = url.replace('__id__', id);
    }

</script>

<h2>ViewPendingTests</h2>

@(Html.Kendo().Grid(Model.PendingTests)
    .Name("pendingTestsGrid")
    .Columns(cols =>
        {
            cols.Bound(x => x.PendingTestId).Hidden(true);
            cols.ForeignKey(x => x.GroupId, Model.Groups, "GroupId", "GroupName").Title("Group");
            cols.ForeignKey(x => x.TestId, Model.Tests, "TestId", "TestName").Title("Test");
            cols.Bound(x => x.ExpirationDate);
            cols.Command(cmd => cmd.Edit());
            cols.Command(cmd => cmd.Destroy());
        })
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
    .ToolBar(cmd => cmd.Create())
    .Groupable()
    .DataSource(dataSource => dataSource
        .Server()
        .Model(model =>
            {
                model.Id(x=>x.PendingTestId);
                model.Field(x => x.GroupId).Editable(false);
                model.Field(x => x.TestId).Editable(false);
            })
        .Create(create => create.Action("CreatePendingTest", "Admin"))
        .Update(update => update.Action("EditPendingTest", "Admin"))
        .Destroy(destroy => destroy.Action("DeletePendingTest", "Admin"))
        )
    .Groupable()
      )
